package com.steven.android40_weathermvp.model;

import com.steven.android40_weathermvp.bean.WeatherBean;
import com.steven.android40_weathermvp.helper.OkHttp3Helper;
import com.steven.android40_weathermvp.retrofitinterface.RetrofitInterface;
import com.steven.android40_weathermvp.utils.URLConstant;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;


public class WeatherModelImpl implements IWeatherModel {
    private static final String TAG = "WeatherModelImpl";
    private Retrofit mRetrofit = null;

    //初始化Retrofit
    private void initRetrofit() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(URLConstant.URL_WEATHER_BASE)
                .client(OkHttp3Helper.getOkHttpSingletonInstance())
                .addConverterFactory(GsonConverterFactory.create())
                // Retrofit2+RxJava实现网络访问，必须设置以下选项。
                // RxJavaCallAdapterFactory就是把Call转换成Observable.
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    @Override
    public Observable<WeatherBean> loadWeatherData(String city) {
        //初始化Retrofit
        initRetrofit();
        return mRetrofit.create(RetrofitInterface.class).getBeanObservable(city);
    }
}
